<?php

/**
 * This is the model class for table "exam".
 *
 * The followings are the available columns in table 'exam':
 * @property integer $id
 * @property integer $start
 * @property integer $exam_time
 * @property string $exam_name
 * @property integer $total_time
 * @property integer $user_id
 * @property integer $total_right
 * @property integer $total_question
 * @property string $params
 * @property int $course_id
 * @property float $total_mark
 * @property float $extra_mark
 * @property string $file_exam
 * @property string $full_name
 * @property string $depart_name
 */
class Exam extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Exam the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'exam';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('start, exam_time, total_time, user_id', 'numerical', 'integerOnly'=>true),
			array('extra_mark', 'numerical', 'integerOnly'=>true, 'max'=>50, 'tooBig'=>'Điểm nhỏ hơn 50'),
            array('file_exam', 'length', 'max'=>512),
            array('full_name, depart_name', 'length', 'max'=>512),
            // The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, start, exam_time, total_time, user_id, total_right, total_question, params, exam_name, total_mark, course_id, extra_mark, file_exam, full_name, depart_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'YumUser', 'user_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'start' => 'Start',
			'exam_time' => 'Exam Time',
			'total_time' => 'Total Time',
			'user_id' => 'User',
            'extra_mark' => 'Extra mark',
            'file_exam' => 'File'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('start',$this->start);
		$criteria->compare('exam_time',$this->exam_time);
		$criteria->compare('total_time',$this->total_time);
		$criteria->compare('user_id',$this->user_id);
        $criteria->order = '(total_mark + extra_mark) desc';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getMark(){
        if ($this->total_question == 0) return 0;
        return $this->total_right;
		$t = round($this->total_right / $this->total_question * 10);
		$m = ($t * 10) % 10;
		if ($m != 0){
			$t = $m < 5 ? $t : $t + 0.5;
		}
		return $t;	
	}
	
	public function getTimeExam(){
		$time = $this->exam_time - $this->start;
		return Util::sec_to_time($time);
	}
	
	/**
	 * Set value params
	 * @param array $params
	 */
	public function setParams($params){
		if (empty($this->params))
			$tparams = array();
		else
			$tparams = json_decode($this->params);
		
		foreach ($params as $key => $val){
			$tparams[$key] = $val;
		}
		$this->params = json_encode($tparams);
	}
	
	/**
	 * Get value item in params
	 * @param string $key
	 */
	public function getParams($key){
		if (empty($this->params)) return "";
		$params = json_decode($this->params, true);
		
		if (is_array($params) && array_key_exists($key, $params))
			return $params[$key];
		return "";
	}
	
	public function getData(){
		$criteria = new CDbCriteria;
		$criteria->order = '(total_mark + extra_mark) desc';
		return $this->findAll($criteria);
	}

    public function checkExam($user_id, $course_id){
        $data = $this->findByAttributes(array('user_id' => $user_id, 'course_id' => $course_id));
        if ($data) return false;
        return true;
    }
}